package com.wzh.himalaya.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzh.himalaya.R;
import com.wzh.himalaya.base.BaseApplication;
import com.wzh.himalaya.views.PopWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

/**
 * className:PlayListAdapter
 * Author:WZH
 * Time:2022-05-09
 */
public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.InnerHolder> {

    private List<Track> mDetailData = new ArrayList<>();
    private int playingIndex = 0;
    private PopWindow.playListItemClickListener mItemClickListener= null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView  =  LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list,parent,false);

        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });




        //设置数据
        Track track = mDetailData.get(position);
        TextView trackTitleTv = holder.itemView.findViewById(R.id.track_title_tv);

        //如果是当前播放的那个音频，颜色改成橙色
        trackTitleTv.setTextColor(playingIndex == position
                ? BaseApplication.getAppContext().getColor(R.color.bg_red)
                :BaseApplication.getAppContext().getColor(R.color.play_list_text_color));



        trackTitleTv.setText(track.getTrackTitle());
        //找到播放的状态
        View playingIconView = holder.itemView.findViewById(R.id.play_icon_iv);
        playingIconView.setVisibility(playingIndex==position ? View.VISIBLE:View.GONE);

    }

    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> data) {

        //清除原来的数据
        mDetailData.clear();
        //添加新的数据
        mDetailData.addAll(data);
        //更新UI
        notifyDataSetChanged();

    }

    public void setCurrentPlayPosition(int position) {
        playingIndex = position;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(PopWindow.playListItemClickListener listener) {
       this.mItemClickListener=listener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}

